package com.owen.java.demo.jsql;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;

public class WhereClause {

    public String getWhere() throws JSQLParserException {
        String sql="SELECT col1 AS a, col2 AS b, col3 AS c FROM table WHERE col_1 = 10 AND col_2 = 20 AND col_3 = 30";
        Select stmt = (Select) CCJSqlParserUtil.parse(sql);
        System.out.println("before " + stmt.toString());

        ((PlainSelect)stmt.getSelectBody()).getWhere().accept(new ExpressionVisitorAdapter() {
            @Override
            public void visit(Column column) {
                column.setColumnName(column.getColumnName().replace("_", ""));
            }
        });

        System.out.println("after " + stmt);

        return stmt.toString();
    }

    public static void main(String[] args) {
        try {
            new WhereClause().getWhere();
        } catch (JSQLParserException e) {
            e.printStackTrace();
        }
    }
}
